package com.bootdo.system.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.aspect.WebLogAspect;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.JSONUtils;
import com.bootdo.common.utils.MD5Utils;
import com.bootdo.system.domain.SecurityDo;
import com.bootdo.system.service.SecurityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.alibaba.druid.util.Utils.md5;


//防伪Controller
@RestController
public class SecurityController {

    @Autowired
    SecurityService securityService;

    private static String Key = "SECURITY-";
    private static final Logger logger = LoggerFactory.getLogger(SecurityController.class);


    @GetMapping("/getCode")
    public String getCode(String key){
        Map map = new HashMap();
        try{
            String currDate = DateUtils.formats(new Date());
            String keys = md5(Key+currDate);
            logger.info("getCode key : "+key  +";  keys : "+keys+" currTime : "+currDate);

            if(key.equals(keys) == false){
                map.put("status","400");
                map.put("message","验签失败,请求失败");
                return JSONUtils.beanToJson(map);
            }

            SecurityDo codeDo = securityService.getCodeDo();
            if(codeDo != null) {
                Long code = codeDo.getCode();
                map.put("status","200");
                map.put("message","请求成功");
                map.put("code",code);
            } else {
                map.put("status","500");
                map.put("message","防伪编号生成失败");
            }
        }catch (Exception e) {
            map.put("status","100");
            map.put("message","系统崩溃了，请稍后再试。");
            logger.error("getCode is error, error : "+e);
            return JSONUtils.beanToJson(map);
        };
        return JSONUtils.beanToJson(map);
    }

    @GetMapping("/queryByCode")
    public String queryByCode(String key , Long code){
        Map map = new HashMap();
        try {
            String currTime = DateUtils.formats(new Date());
            String keys = md5(Key+currTime);
            logger.info("queryByCode key : "+key  +";  keys : "+keys+" currTime : "+currTime);
            if(key.equals(keys) == false){
                map.put("status","400");
                map.put("message","验签失败,请求失败");
                return JSONUtils.beanToJson(map);
            }
            int i = securityService.queryByCode(code);
            if(securityService.queryByCode(code)>0){
                map.put("status","200");
                map.put("message","请求成功");
                map.put("code",code);
                Date date = new Date();
                String currDate = DateUtils.format(date);
                map.put("currDate",currDate);
            }else {
                map.put("status","300");
                map.put("message","该防伪编号错误");
            }
        }catch (Exception e) {
            map.put("status","100");
            map.put("message","系统崩溃了，请稍后再试。");
            logger.error("getCode is error, error : "+e);
            return JSONUtils.beanToJson(map);
        };
        return JSONUtils.beanToJson(map);
    }
/*    @GetMapping("/getCode")

    public String getCode() {
        String appid = "wx42d33e2522cf818e";
        //safety_domain_name为域名
        String safety_domain_name = "www.mubole.top";
        //scope为snsapi_base则不会出现需用户点击损权的另单亩，但如果在用户没关主微信公众号的前提下是无法获得微信名和用户头像
        String queryURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&"
                + "redirect_uri=http://" + safety_domain_name
                + "＆response type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
        return "redirect:" + queryURL;
    }*/
}
